package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.vo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    AddressMapper addressMapper;

    @Autowired
    CollectMapper collectMapper;
    @Autowired
    FootprintMapper footprintMapper;

    @Autowired
    SearchHistoryMapper searchHistoryMapper;
    @Autowired
    FeedbackMapper feedbackMapper;

    @Override
    public UserVo queryList(Integer page, Integer limit, String sort, String order, String username, String mobile) {

        PageHelper.startPage(page, limit); //mybatis在构建sql语句 👉 会构造limit和offset
        UserExample userExample = new UserExample();
        userExample.setOrderByClause(sort + " " + order);//${}做Sql拼接


        System.out.println(sort + " " + order);

        UserExample.Criteria criteria = userExample.createCriteria();
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (mobile != null && !"".equals(mobile)) {
            criteria.andMobileLike("%" + mobile + "%");
        }

        List<User> users = userMapper.selectByExample(userExample);


        PageInfo<User> userPageInfo = new PageInfo<>(users);//查询结果放进来
        long total = userPageInfo.getTotal();//总的数据量 👉 select count(*) 条件是byExample

        UserVo userVo = new UserVo();
        userVo.setItems(users);
        userVo.setTotal(total);
        return userVo;

    }

    @Override
    public AddressVO queryAddress(Integer page, Integer limit, String sort, String order, String name, Integer userId) {

        PageHelper.startPage(page, limit); //mybatis在构建sql语句 👉 会构造limit和offset

        AddressExample addressExample = new AddressExample();

        addressExample.setOrderByClause(sort + " " + order);//根据sort排序

        AddressExample.Criteria criteria = addressExample.createCriteria();

        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (name != null && !"".equals(name)) {
            criteria.andMobileLike("%" + name + "%");
        }
        List<Address> addresses = addressMapper.selectByExample(addressExample);

        PageInfo<Address> addressPageInfo = new PageInfo<>(addresses);

        long total = addressPageInfo.getTotal();

        AddressVO addressVO = new AddressVO();

        addressVO.setTotal(total);

        addressVO.setItems(addresses);

        return addressVO;
    }

    @Override
    public CollectVO queryCollect(Integer page, Integer limit, String sort, String order, Integer userId, Integer valueId) {

        PageHelper.startPage(page, limit); //mybatis在构建sql语句 👉 会构造limit和offset
        CollectExample collectExample = new CollectExample();
        collectExample.setOrderByClause(sort + " " + order);//${}做Sql拼接
        CollectExample.Criteria criteria = collectExample.createCriteria();
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null && !"".equals(valueId)) {
            criteria.andUserIdEqualTo(valueId);
        }
        List<Collect> collects = collectMapper.selectByExample(collectExample);

        //total != users.size();
        //total是满足条件的所有的数据量 👉 而不是当前的limit
        //可以通过PageInfo获得分页信息
        PageInfo<Collect> collectPageInfo = new PageInfo<>(collects);//查询结果放进来
        long total = collectPageInfo.getTotal();//总的数据量 👉 select count(*) 条件是byExample

        CollectVO collectVO = new CollectVO();
        collectVO.setTotal(total);
        collectVO.setItems(collects);
        return collectVO;
    }

    @Override
    public FootprintVO queryFootprint(Integer page, Integer limit, String sort, String order, Integer userId, Integer goodsId) {
        PageHelper.startPage(page, limit); //mybatis在构建sql语句 👉 会构造limit和offset


        FootprintExample footprintExample = new FootprintExample();
        footprintExample.setOrderByClause(sort + " " + order);
        FootprintExample.Criteria criteria = footprintExample.createCriteria();

        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (goodsId != null && !"".equals(goodsId)) {
            criteria.andGoodsIdEqualTo(goodsId);
        }

        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);


        PageInfo<Footprint> footprintPageInfo = new PageInfo<Footprint>(footprints);//查询结果放进来
        long total = footprintPageInfo.getTotal();//总的数据量 👉 select count(*) 条件是byExample
        FootprintVO footprintVO = new FootprintVO();
        footprintVO.setTotal(total);
        footprintVO.setItems(footprints);
        return footprintVO;
    }

    @Override
    public HistoryVO queryHistory(Integer page, Integer limit, String sort, String order, String keyword, Integer userId) {
        PageHelper.startPage(page, limit); //mybatis在构建sql语句 👉 会构造limit和offset

        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        searchHistoryExample.setOrderByClause(sort + " " + order);

        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();

        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (keyword != null && !"".equals(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
        PageInfo<SearchHistory> searchHistoryPageInfo = new PageInfo<>(searchHistories);//查询结果放进来
        long total = searchHistoryPageInfo.getTotal();
        HistoryVO historyVO = new HistoryVO();
        historyVO.setTotal(total);
        historyVO.setItems(searchHistories);
        return historyVO;
    }


    @Override
    public FeedbackVO feedback(Integer page, Integer limit, String username, String sort, String order, Integer id) {
//        PageHelper.startPage(page, limit); //mybatis在构建sql语句 👉 会构造limit和offset
//        FeedbackExample feedbackExample = new FeedbackExample();
//
//        feedbackExample.setOrderByClause(sort + " " + order);
//
//        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
//
//        if(username!= null && !"".equals(username)){
//            criteria.andUsernameLike("%" + username + "%");
//        }
//
//    }

        return null;
    }
}
